/*********************************************************************
*
* Software License Agreement (BSD License)
*
*  Copyright (c) 2010, Heriot-Watt University, UK.
*  All rights reserved.
*
*  Redistribution and use in source and binary forms, with or without
*  modification, are permitted provided that the following conditions
*  are met:
*
*   * Redistributions of source code must retain the above copyright
*     notice, this list of conditions and the following disclaimer.
*   * Redistributions in binary form must reproduce the above
*     copyright notice, this list of conditions and the following
*     disclaimer in the documentation and/or other materials provided
*     with the distribution.
*   * Neither the name of the Heriot-Watt University nor the names of
*     its contributors may be used to endorse or promote products
*     derived from this software without specific prior written
*     permission.
*
*  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
*  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
*  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
*  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
*  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
*  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
*  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
*  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
*  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
*  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
*  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
*  POSSIBILITY OF SUCH DAMAGE.
*
*  Author: Joel Cartwright
*
*********************************************************************/

#include <matchmaker/service_monitor.h>

namespace matchmaker
{

// ------------------------------------------------------------
// Public methods
// ------------------------------------------------------------

ServiceMonitor::ServiceMonitor()
{

}


ServiceMonitor::ServiceMonitor(const ServiceMonitor& rhs)
: impl_(rhs.impl_)
{

}


ServiceMonitor::~ServiceMonitor()
{

}


void ServiceMonitor::shutdown()
{
  impl_.reset();
}


std::string ServiceMonitor::getID() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::getId called on null monitor.");
    return std::string();
  }

  return impl_->service_.id;
}


std::string ServiceMonitor::getName() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::getName called on null monitor.");
    return std::string();
  }

  return impl_->service_.name;
}


std::string ServiceMonitor::getTopic() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::getTopic called on null monitor.");
    return std::string();
  }

  return impl_->service_.topic;
}


ServiceStatus ServiceMonitor::getStatus() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::setStatus called on null monitor.");
    return ServiceStatus(ServiceStatus::REMOVED, "null monitor");
  }

  return impl_->service_.status;
}


std::string ServiceMonitor::getProvider() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::getProvider called on null monitor.");
    return std::string();
  }

  return impl_->service_.provider;
}


const ServiceDetails* ServiceMonitor::getServiceDetails() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::getServiceDetails called on null monitor.");
    return NULL;
  }

  return &impl_->service_;
}


ros::Time ServiceMonitor::getStatusChangeTime() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::getStatusChangeTime called on null monitor.");
    return ros::Time();
  }

  return impl_->statusChangeTime_;
}


// ------------------------------------------------------------
// Private methods
// ------------------------------------------------------------

ServiceMonitor::Impl::Impl()
: creationTime_(ros::Time::now())
{

}


ServiceMonitor::Impl::~Impl()
{

}


ServiceMonitor::ServiceMonitor(const ServiceDetailsPtr& service,
    boost::function<void (const ServiceDetails& service)> callback)
: impl_(new Impl())
{
  // Create a deep copy of the service details.
  impl_->service_ = *(service.get());
  impl_->callback_ = callback;
}


ServiceMonitor::ServiceMonitor(const ServiceDetails* service,
    boost::function<void (const ServiceDetails& service)> callback)
: impl_(new Impl())
{
  // Create a deep copy of the service details.
  impl_->service_ = *service;
  impl_->callback_ = callback;
}


void ServiceMonitor::storeTimer(ros::Timer timer)
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::storeTimer called on null monitor.");
    return;
  }

  impl_->callbackTimer_ = timer;
}


void ServiceMonitor::setStatus(const ServiceStatus& status)
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::setStatus called on null monitor.");
    return;
  }

  if (status != impl_->service_.status || impl_->statusChangeTime_.isZero()) {
    impl_->statusChangeTime_ = ros::Time::now();
  }

  impl_->service_.status = status;
}


void ServiceMonitor::performCallback()
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::performCallback called on null monitor.");
    return;
  }

  impl_->callback_(impl_->service_);
}


ros::Time ServiceMonitor::getCreationTime() const
{
  if (!impl_) {
    ROS_WARN_STREAM("ServiceMonitor::getCreationTime called on null monitor.");
    return ros::Time();
  }

  return impl_->creationTime_;
}


long int ServiceMonitor::getNumCopies()
{
   if (!impl_) return 0;

   return impl_.use_count();
}


} // namespace
